package com.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.domain.SysUser;
import com.demo.domain.Vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author gxd
 * @since 2025/5/27
 */
public interface SysUserMapper extends BaseMapper<SysUser> {

    @Select("<script> " +
            "SELECT t1.dept_name, t2.* " +
            "FROM sys_dept t1 " +
            "JOIN sys_user t2 ON t1.dept_id = t2.dept_id " +
            "<where>" +
            "  <if test=\"user.userName != null and user.userName != ''\"> " +
            "    AND t2.user_name LIKE concat('%', #{user.userName}, '%') " +
            "  </if> " +
            "  <if test=\"user.nickName != null and user.nickName != ''\"> " +
            "    AND t2.nick_name LIKE concat('%', #{user.nickName}, '%') " +
            "  </if> " +
            "  <if test=\"user.status != null\"> " +
            "    AND t2.status = #{user.status}" +
            "  </if> " +
            "</where> " +
            "</script>")
    List<SysUser> pageList(@Param("user") SysUser user);

    @Select("<script> " +
            "SELECT t1.dept_name, t2.* " +
            "FROM sys_dept t1 " +
            "JOIN sys_user t2 ON t1.dept_id = t2.dept_id " +
            "<where> " +
            "  <if test=\"user.userName != null and user.userName != ''\"> " +
            "    AND t2.user_name LIKE concat('%', #{user.userName}, '%') " +
            "  </if> " +
            "  <if test=\"user.nickName != null and user.nickName != ''\"> " +
            "    AND t2.nick_name LIKE concat('%', #{user.nickName} ,'%') " +
            "  </if> " +
            "  <if test=\"user.status != null\"> " +
            "    AND t2.status = #{user.status}" +
            "  </if> " +
            "</where> " +
            "</script>")
    List<SysUser> selectUserList(@Param("user") SysUser user);

    @Select("<script> " +
            "SELECT t1.*, IFNULL(t2.dept_name, '无') deptName, t3.name sexMc " +
            "FROM sys_user t1 " +
            "LEFT JOIN sys_dept t2 ON t1.dept_id = t2.dept_id " +
            "JOIN sys_dict t3 ON t3.parent_code = 'USER-SEX' AND t1.sex = t3.code " +
            "WHERE t1.del_flag = '0' " +
            "</script>")
    List<SysUser> selectUserJoin();

    @Select("<script> " +
            "SELECT \n" +
            "    CONCAT(\n" +
            "        LPAD(HOUR(create_time) + IF(MINUTE(create_time) &lt; 30, 0, 1), 2, '0'), \n" +
            "        ':',\n" +
            "        IF(MINUTE(create_time) &lt; 30, '30', '00')\n" +
            "    ) AS time,\n" +
            "    COUNT(user_id) AS count\n" +
            "FROM sys_user\n" +
            "WHERE `status` = '0' AND del_flag = '0' AND create_time >= #{startTime} AND create_time &lt;= #{endTime}\n" +
            "GROUP BY \n" +
            "    DATE(create_time),\n" +
            "    HOUR(create_time),\n" +
            "    FLOOR(MINUTE(create_time)/30)\n" +
            "ORDER BY \n" +
            "    DATE(create_time),\n" +
            "    HOUR(create_time),\n" +
            "    FLOOR(MINUTE(create_time)/30);" +
            "</script>")
    List<UserVo> selectUserBatch(@Param("startTime") String startTime, @Param("endTime") String enTime);

    @Select("<script> " +
            "SELECT nick_name AS nickName " +
            "FROM sys_user " +
            "<where> " +
            "   status = '0' AND del_flag = '0' " +
            "   <if test=\"user.pinyin != null and user.pinyin != ''\"> " +
            "      AND (pinyin LIKE concat('%', #{user.pinyin}, '%') or pinyin_sort LIKE concat('%', #{user.pinyin}, '%'))" +
            "   </if>" +
            "</where>" +
            "</script>")
    List<String> selectUserNameByPinyin(@Param("user") SysUser user);
}
